package edu.biwu;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * ServletContext中的方法
 *   String getRealPath():传入一个类的路径,然后返回一个资源的真实路径
 *
 */
@WebServlet("/context01")
public class ServletContextDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = getServletContext();
        System.out.println(servletContext);
        //db.properties在tomcat上路径,以target目录(tomcat部署的就是target下的应用)下的类路径起始
        // /开始,代表整个应用
        String realPath = servletContext.getRealPath("/WEB-INF/classes/db.properties");
        System.out.println(realPath);

        //将db.properties中的键值对加载到Properties集合中
        Properties properties = new Properties();
        properties.load(new FileInputStream(realPath));
        System.out.println(properties);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
